package cn.jinjimi.app.custom.ui.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v4.content.ContextCompat;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import cn.jinjimi.base.util.DateUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.custom.entity.Custom;
import cn.jinjimi.app.store.entity.custom.LastFollow;
import cn.jinjimi.app.main.adapter.extend.MyBaseAdapter;
import cn.jinjimi.app.main.widget.FrescoImageView.FrescoImageView;

import static cn.jinjimi.app.R.id.tv_sec_title;

/**
 * 准客户列表adapter
 * <p/>
 * Created by jinsen on 15/10/22.
 */
public class CustomerTypeAdapter extends MyBaseAdapter<Custom> {

    private Context mContext;
    private Paint mPaint;
    private Paint mPaint2;

    private int titleLength;

    public CustomerTypeAdapter(Context context) {
        this.mContext = context;
        mPaint = new Paint();
        mPaint2 = new Paint();
        mPaint.setTextSize(mContext.getResources().getDimension(R.dimen.font_14));
        mPaint2.setTextSize(mContext.getResources().getDimension(R.dimen.font_10));
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Holder holder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_insure_follow_plan_list, null);
            holder = new Holder(convertView);

            convertView.findViewById(R.id.tv_third_title).setVisibility(View.GONE);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        final Custom custom = getItem(position);

        if (titleLength == 0) {
            ViewTreeObserver vto2 = holder.name.getViewTreeObserver();
            vto2.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    holder.name.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    titleLength = holder.name.getWidth();
                    holder.setTitle(custom);
                }
            });
        } else {
            holder.setTitle(custom);
        }

        LastFollow lastFollow = custom.getLastFollow();
        if (lastFollow != null) {
            holder.tvTime.setText(DateUtils.formatyyyymmddhhmm(lastFollow.getFollowTime()));
            holder.tvContent.setText(lastFollow.getContent());
            holder.tvContent.setVisibility(View.VISIBLE);
        } else {
            holder.tvTime.setText("暂无跟进记录");
            holder.tvContent.setVisibility(View.GONE);
        }

        holder.avater.loadView(custom.avatar, R.drawable.list_icon_portrait);
        holder.avater.asCircle();

        return convertView;
    }

    class Holder {
        FrescoImageView avater;
        TextView name;
        TextView tvTime;
        TextView tvContent;

        public Holder(View view) {
            avater = (FrescoImageView) view.findViewById(R.id.img);
            name = (TextView) view.findViewById(R.id.tv_title);
            tvTime = (TextView) view.findViewById(tv_sec_title);
            tvContent = (TextView) view.findViewById(R.id.tv_third_title);
        }

        /* 设置标题 */
        public void setTitle(Custom item) {
            if (item.hasTop()) {
                String topString = "  (置顶)";
                float widthName = mPaint.measureText(item.name);
                float widthTop = mPaint2.measureText(topString);

                SpannableStringBuilder sb = new SpannableStringBuilder(item.name);
                if (widthName < titleLength && widthName + widthTop > titleLength)
                    sb.append("\n");
                sb.append(topString);
                sb.setSpan(new AbsoluteSizeSpan(10, true), item.name.length(), sb.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                sb.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.text_green)), item.name.length(), sb.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                name.setText(sb);
            } else {
                name.setText(item.name);
            }
        }
    }

}
